﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Controls;
using System.IO;

namespace ComUIControl
{
    public partial class PathSelector : MetroUserControl, INotifyPropertyChanged
    {
        private PathType _typePath = PathType.File;
        [DefaultValue(PathType.File)]
        public PathType TypePath
        {
            get
            {
                return _typePath;
            }
            set
            {
                _typePath = value;
            }
        }

        private string _filter = "";
        [DefaultValue("")]
        public string Filter
        {
            get
            {
                return _filter;
            }
            set
            {
                _filter = value;
            }
        }

        private string _path;
        [DefaultValue("")]
        public string SelectedPath
        {
            get
            {
                return _path;
            }
            set
            {
                if (_path != value)
                {
                    _path = value;
                    if (!this.InvokeRequired)
                    {
                        metroTextBoxFilePath.Text = value;
                    }
                    this.RaisePropertyChanged("SelectedPath");
                }
            }
        }

        public PathSelector()
        {
            InitializeComponent();
            metroTextBoxFilePath.DataBindings.Add("Text", this, "SelectedPath");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public delegate void UpdatePropertyDelegate(string strName);
        private void RaisePropertyChanged(string propertyName)
        {
            if (this.InvokeRequired)
            {
                UpdatePropertyDelegate outdelegate = new UpdatePropertyDelegate(RaisePropertyChanged);
                this.BeginInvoke(outdelegate, new object[] { propertyName });
                return;
            }
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void metroTextBoxFilePath_ButtonClick(object sender, EventArgs e)
        {
            if(_typePath == PathType.File)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = Environment.CurrentDirectory;
                openFileDialog.Filter = Filter;
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FilterIndex = 1;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    SelectedPath = openFileDialog.FileName;
                }
            }
            if (_typePath == PathType.Folder)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.RootFolder = Environment.SpecialFolder.MyComputer;
                dialog.Description = "请选择文件路径";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    SelectedPath = dialog.SelectedPath;
                }
            }
        }

        private void metroTextBoxFilePath_Validating(object sender, CancelEventArgs e)
        {
            string path = metroTextBoxFilePath.Text;
            if(TypePath == PathType.File)
            {
                if (!File.Exists(path))
                {
                    string path0 = string.Format(@"{0}/{1}", Environment.CurrentDirectory, path);
                    if (!File.Exists(path0))
                    {
                        string infoErr = "不存在的文件路径!";
                        errorProvider1.SetError(metroTextBoxFilePath, infoErr);
                        e.Cancel = true;
                        return;
                    }
                }               
            }
            if (TypePath == PathType.Folder)
            {
                if (!Directory.Exists(path))
                {
                    string path0 = string.Format(@"{0}/{1}", Environment.CurrentDirectory, path);
                    if (!Directory.Exists(path0))
                    {
                        string infoErr = "不存在的路径!";
                        errorProvider1.SetError(metroTextBoxFilePath, infoErr);
                        e.Cancel = true;
                        return;
                    }
                }
            }
            e.Cancel = false;
            errorProvider1.SetError(metroTextBoxFilePath, "");
        }

    };

    public enum PathType : uint
    {
        Folder = 0,
        File
    }
}
